package org.jeecg.modules.crm.service.impl;

import org.jeecg.modules.crm.entity.ManagerCommission;
import org.jeecg.modules.crm.entity.ManagerCommissionDateil;
import org.jeecg.modules.crm.mapper.ManagerCommissionDateilMapper;
import org.jeecg.modules.crm.mapper.ManagerCommissionMapper;
import org.jeecg.modules.crm.service.IManagerCommissionService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: Manager Commission
 * @Author: jeecg-boot
 * @Date:   2025-04-10
 * @Version: V1.0
 */
@Service
public class ManagerCommissionServiceImpl extends ServiceImpl<ManagerCommissionMapper, ManagerCommission> implements IManagerCommissionService {

	@Autowired
	private ManagerCommissionMapper managerCommissionMapper;
	@Autowired
	private ManagerCommissionDateilMapper managerCommissionDateilMapper;
	
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void saveMain(ManagerCommission managerCommission, List<ManagerCommissionDateil> managerCommissionDateilList) {
		managerCommissionMapper.insert(managerCommission);
		if(managerCommissionDateilList!=null && managerCommissionDateilList.size()>0) {
			for(ManagerCommissionDateil entity:managerCommissionDateilList) {
				//外键设置
				entity.setMainId(managerCommission.getId());
				managerCommissionDateilMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateMain(ManagerCommission managerCommission,List<ManagerCommissionDateil> managerCommissionDateilList) {
		managerCommissionMapper.updateById(managerCommission);
		
		//1.先删除子表数据
		managerCommissionDateilMapper.deleteByMainId(managerCommission.getId());
		
		//2.子表数据重新插入
		if(managerCommissionDateilList!=null && managerCommissionDateilList.size()>0) {
			for(ManagerCommissionDateil entity:managerCommissionDateilList) {
				//外键设置
				entity.setMainId(managerCommission.getId());
				managerCommissionDateilMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delMain(String id) {
		managerCommissionDateilMapper.deleteByMainId(id);
		managerCommissionMapper.deleteById(id);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			managerCommissionDateilMapper.deleteByMainId(id.toString());
			managerCommissionMapper.deleteById(id);
		}
	}
	
}
